package com.alinesno.cloud.portal.auto.service.impl;

import java.util.Date;

import javax.validation.constraints.NotBlank;

import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.alinesno.cloud.base.boot.entity.ManagerSettingsEntity;
import com.alinesno.cloud.base.boot.service.IManagerSettingsService;
import com.alinesno.cloud.common.core.constants.SpringInstanceScope;
import com.alinesno.cloud.common.core.service.impl.IBaseServiceImpl;
import com.alinesno.cloud.portal.auto.constants.AutoConstants;
import com.alinesno.cloud.portal.auto.entity.AutoTaskEntity;
import com.alinesno.cloud.portal.auto.entity.AutoTaskHistoryEntity;
import com.alinesno.cloud.portal.auto.service.IAutoTaskHistoryService;
import com.alinesno.cloud.portal.auto.service.IAutoTaskService;
import com.alinesno.cloud.portal.auto.task.AsyncTaskManager;
import com.alinesno.cloud.portal.auto.task.TaskInfo;
import com.alinesno.cloud.portal.auto.tool.maven.MvnProjectBuilder;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WeiXiaoJin
 * @since 2019-11-21 07:41:47
 */
@Scope(SpringInstanceScope.PROTOTYPE)
@Service
public class AutoTaskServiceImpl extends IBaseServiceImpl<AutoTaskEntity, String> implements IAutoTaskService {

	// 日志记录
	private static final Logger log = LoggerFactory.getLogger(AutoTaskServiceImpl.class);

	private MvnProjectBuilder b = new MvnProjectBuilder() ; 

	@Reference
	private IManagerSettingsService managerSettingsService ; 
	
	// 注入异步任务管理器
	@Autowired
	private AsyncTaskManager asyncTaskManager;
	
	@Autowired
	private IAutoTaskHistoryService taskHistory ; 

	@Override
	public void runGatlingTask(@NotBlank(message = "主键不能为空.") String id) {
		
		// 1. 调整任务状态
		AutoTaskEntity e = findById(id).get();
		Assert.isTrue(!AutoConstants.RunStatus.RUN.equals(e.getRunStatus()), "任务还在运行中.");
		
		TaskInfo taskInfo = asyncTaskManager.submit(() -> {
			// 1. 获取全局配置
			ManagerSettingsEntity settings = managerSettingsService.queryKey("auto.tools.mvn", null) ; 
			
			e.setRunStatus(AutoConstants.RunStatus.RUN);
			e.setTaskVersion(e.getTaskVersion()==null?1:(e.getTaskVersion()+1));
			save(e);

			// 2. 生成构建任务历史
			AutoTaskHistoryEntity eHis = new AutoTaskHistoryEntity() ; 
			BeanUtils.copyProperties(e, eHis); 
			eHis.setTaskId(e.getId());
			taskHistory.save(eHis) ; 
			
			// 3. 运行构建
			try {
				String version = eHis.getTaskVersion()+"" ; 
				String jobName = e.getId() ; 
				String mvnHome = settings.getConfigValue() ; 
				String scalaCode = e.getScriptContent() ; 
				
				b.createGatlingMavenJob(null, version, jobName, mvnHome , scalaCode) ; 
				e.setRunStatus(AutoConstants.RunStatus.SUCCESS);
				e.setLastSuccessTime(new Date());
				save(e);
			} catch (Exception ex) {
				e.setRunStatus(AutoConstants.RunStatus.FAIL);
				e.setLastFailTime(new Date());
				save(e);
				log.debug("ex:{}" , ex);
			}
		});
		
		log.debug("taskInfo:{}" , taskInfo);
	}

}
